#include "activehandler.h"

ActiveHandler::ActiveHandler(VPxDecoder *decoder,QObject *parent ) : NetWorkHandler(parent)
    ,m_isAuthenticated(false)
{
    this->m_VpxDecoder = decoder ;
}
ActiveHandler::~ActiveHandler(){

}

void ActiveHandler::createToHandler(){

}
void ActiveHandler::dealProto(int i,BigPack::Exchange resv_exc){
    switch(i){
    case BigPack::Exchange::TypeReplyRegisterDetails:{
        if(resv_exc.replyinfo().success()){
            this->m_transferId = QString::fromStdString(resv_exc.replyinfo().registerid());
            queryHost(m_ntInfo->getRemoteDeviceID());
        }
        break;
    }
    case BigPack::Exchange::TypeResponseHost:{
        if(resv_exc.responsehost().isexist()){
            emit showInputPasswdDlg();
        }else{
            emit remoteConnectError("远程设备不在线");
        }
        break;
    }
    case BigPack::Exchange::TypeResponseAuth:{
        if(resv_exc.responseauth().success()){
            this->targetId= QString::fromStdString(resv_exc.resourceid());
            m_isAuthenticated = true ;
        }else{
            emit remoteConnectError("密码验证失败");
        }
        break;
    }
    case BigPack::Exchange::TypeImageParameters:{
        BigPack::ScImageParameters imagePara=resv_exc.imageparameters() ;
        emit imageParameters(imagePara.imagewidth(),imagePara.imageheight());
        break;
    }
    case  BigPack::Exchange::TypeImage:{
        BigPack::ScImageInfo imgInfo = resv_exc.image() ;
        int pox = imgInfo.posx();
        int poy = imgInfo.posy();
        int width =  imgInfo.imagewidth();
        int height = imgInfo.imageheight();
        QByteArray in(imgInfo.m_data().c_str(),imgInfo.m_data().size());
        m_VpxDecoder->decode_(pox,poy,width,height,&in);
        break;
    }

    }
}
void ActiveHandler::slotSendMouseMove(QPoint point){
    if(!m_isAuthenticated){
        return ;
    }
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeMouseMove);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targetId.toStdString());

    BigPack::CsMouseMove *mm = new BigPack::CsMouseMove;
    mm->set_posx(point.x());
    mm->set_posy(point.y());
    ex.set_allocated_mousemove(mm);
    serializeSend(ex);
}
void ActiveHandler::slotSendKeyboard(int code,bool state){
    if(!m_isAuthenticated){
        return ;
    }
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeKeyBoard);
    ex.set_resourceid(this->m_transferId.toStdString()) ;
     ex.set_targetid(targetId.toStdString());

    BigPack::CsKeyboard *kb = new BigPack::CsKeyboard ;
    kb->set_keycode(code);
    kb->set_state(state);
    ex.set_allocated_keyboard(kb);
    serializeSend(ex);

}
void ActiveHandler::slotSendWheelEvent(bool detal){
    if(!m_isAuthenticated){
        return ;
    }
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeWheelEvent);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targetId.toStdString());

    BigPack::CsWheelEvent *we = new  BigPack::CsWheelEvent;
    we->set_deltapos(detal);
    ex.set_allocated_wheelevent(we);
    serializeSend(ex);
}
void ActiveHandler::slotSendMouseKeys(int code ,bool state){
    if(!m_isAuthenticated){
        return ;
    }
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeMouseKeys);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targetId.toStdString());

    BigPack::CsMouseKeys *mk = new BigPack::CsMouseKeys;
    mk->set_state(state);
    mk->set_keycode(code);
    ex.set_allocated_mousekeys(mk);
    serializeSend(ex);
}
void ActiveHandler::sendleaveLook(){
    if(!m_isAuthenticated){
        return ;
    }
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeRequestLeaveLook);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targetId.toStdString());
    serializeSend(ex);
}

void ActiveHandler::requestDesk(){
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeRequestDesk);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targetId.toStdString());

    BigPack::CsDeskRequest *dq = new BigPack::CsDeskRequest;
    dq->set_operation(1);
    ex.set_allocated_requestdesk(dq);
    serializeSend(ex);

}
void ActiveHandler::slotRequestAuth(QString targetId,QString targetPasswd){

    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeRequestAuth);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targetId.toStdString());

    BigPack::CsRequestAuth * auth = new BigPack::CsRequestAuth();
    auth->set_targetpasswd(targetPasswd.toStdString());
    ex.set_allocated_requestauth(auth);

    serializeSend(ex);

}
void ActiveHandler::queryHost(QString targethostId){
    BigPack::Exchange ex ;
    ex.set_datatype(BigPack::Exchange::TypeQueryHost);
    ex.set_resourceid(m_transferId.toStdString()) ;
    ex.set_targetid(targethostId.toStdString());

    serializeSend(ex);
}
